<template>
  <div :id="id" :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import tdTheme from '@/assets/theme/theme.json'

export default {
  name: "echartTemp",
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    },
    options: {
      type: Object,
      default: ()=>({})
    },
    echartClick: {
      type: Function,
      default: ()=>{}
    },
    echartRightClickFlag: {
      type: Boolean,
      default: false
    },
    echartRightClick: {
      type: Function,
      default: ()=>{}
    }
  },
  data () {
    return {
      chart: null
    }
  },
  watch: {
    options: {
      handler (options) {
        // 设置true清空echart缓存
        this.chart.setOption(options, true)
      },
      deep: true
    }
  },
  mounted () {
    this.$echarts.registerTheme('tdTheme', tdTheme);
    this.initChart();
  },
  methods: {
    initChart () {
      // 初始化echart
      let that = this
      this.chart = this.$echarts.init(this.$el)
      this.chart.setOption(this.options, true)
      this.chart.on('click', that.echartClick)
      // 添加右击事件
      if (this.echartRightClickFlag) {
        document.oncontextmenu = function () { return false; };
        this.chart.on('contextmenu', that.echartRightClick)
      }
    }
  }
}
</script>

<style scoped lang="scss">
</style>
